package com.etime.shuhuiyuan.services.organization.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.OrganizationDao;
import com.etime.shuhuiyuan.pojo.Organization;
import com.etime.shuhuiyuan.services.organization.interfaces.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {

    //用Spring的ioc容器自动注入对象
    @Autowired
    private OrganizationDao organizationDao;

    @Override
    public int insertOrganization(Organization organization) {
        return organizationDao.insert(organization);
    }

    @Override
    public int updateOrganization(Organization organization) {
        return organizationDao.updateById(organization);
    }

    @Override
    public List<Organization> selectOrganizationList(Organization organization) {
        //查询条件
        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        if (organization != null && organization.getOrgname() != null && !organization.getOrgname().equalsIgnoreCase("")) {
            wrapper.eq("orgname", organization.getOrgname());
        }
        return organizationDao.selectList(wrapper);
    }

    @Override
    public Page<Organization> selectOrganizationByPage(Organization organization, long pageNum, long pageSize) {
        //分页条件
        Page<Organization> page = new Page<>(pageNum, pageSize);
        //查询条件
        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        if (organization != null){
//            if(organization.getOrgname() != null && !organization.getOrgname().equalsIgnoreCase("")) {
//                wrapper.eq("orgname", organization.getOrgname());
//            }
            if (organization.getOrgname()!=null && !organization.getOrgname().equalsIgnoreCase("")){
                wrapper.like("orgname",organization.getOrgname());
            }
            if(organization.getOrgauditstate()!=null && organization.getOrgauditstate()!=-1){
                wrapper.eq("orgauditstate",organization.getOrgauditstate());
            }
        }

        return organizationDao.selectPage(page, wrapper);
    }

    @Override
    public Organization selectOrganizationById(Integer organizationId) {
        return organizationDao.selectById(organizationId);
    }
}
